package com.vshop.service.agatewaytobeijing;

import com.alibaba.fastjson.JSON;
import com.vshop.config.dto.StringOutputConstant;
import com.vshop.entity.agatewaytobeijing.JsonConstant;
import com.vshop.entity.agatewaytobeijing.openpackage.request.RequestOpenPackage;
import com.vshop.entity.agatewaytobeijing.openpackage.response.ResponseOpenPackage;
import com.vshop.entity.agatewaytobeijing.querydevice.beijing.request.RequestQueryDevice;
import com.vshop.entity.agatewaytobeijing.querydevice.beijing.response.ResponseQueryDevice;
import com.vshop.entity.agatewaytobeijing.querydeviceorder.request.RequestQueryDeviceOrder;
import com.vshop.entity.agatewaytobeijing.querydeviceorder.response.ResponseQueryDeviceOrder;
import com.vshop.entity.agatewaytobeijing.queryh5order.request.RequestQueryH5Order;
import com.vshop.entity.agatewaytobeijing.queryh5order.response.ResponseQueryH5Order;
import com.vshop.entity.agatewaytobeijing.querypartnercode.request.RequestQueryPartnerByDeviceId;
import com.vshop.entity.agatewaytobeijing.querypartnercode.response.ResponseQueryPartnerByDeviceId;
import com.vshop.entity.agatewaytobeijing.syncdevice.request.RequestSynchronizeDevice;
import com.vshop.entity.agatewaytobeijing.syncdevice.response.ResponseSynchronizeDevice;
import com.vshop.entity.agatewaytobeijing.syncpackage.request.RequestSynchronizePackage;
import com.vshop.entity.agatewaytobeijing.syncpackage.response.ResponseSynchronizePackage;
import com.vshop.entity.agatewaytobeijing.syncpartner.request.RequestSynchronizePartner;
import com.vshop.entity.agatewaytobeijing.syncpartner.response.ResponseSynchronizePartner;
import com.vshop.entity.alast.common.ServicePlanOrderRecord;
import com.vshop.entity.optimize.cancel.request.CancelRequest;
import com.vshop.entity.optimize.cancel.response.CancelResponse;
import com.vshop.entity.optimize.end.request.EndRequest;
import com.vshop.entity.optimize.end.response.EndResponse;
import com.vshop.entity.optimize.order.request.OrderRequest;
import com.vshop.entity.optimize.order.response.OrderResponse;
import com.vshop.entity.optimize.reorder.request.RenewRequest;
import com.vshop.entity.optimize.reorder.response.RenewResponse;
import com.vshop.entity.purchase.request.RequestPurchaseService;
import com.vshop.entity.requestinterface.querypackage.request.RequestPackageQueryNew;
import com.vshop.entity.requestinterface.querypackage.response.ResponsePackageQueryNew;
import com.vshop.service.alast.impl.LogicAssistService;
import com.vshop.utils.HMACSHA1;
import com.vshop.utils.HttpUtil;
import com.vshop.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 所有需要去请求北京企业网关的业务实现类：
 *
 * @author lm
 * @date 2020/11/6 11:48
 */
@Component
@Slf4j
public class GatewayToBeijingService {
    /**
     * 请求深圳iot项目的地址
     */
    @Value("${iot.requestURI}")
    private String requestURI;
    /**
     * 2.0接口：请求北京企业网关的地址
     */
    @Value("${jsonIot2.JSON_BASE_URL}")
    private String jsonBaseURL2;
    /**
     * 3.0接口：请求北京企业网关的地址
     */
    @Value("${jsonIot3.JSON_BASE_URL}")
    private String jsonBaseURL3;

    @Autowired
    LogicAssistService logicAssistService;

    /***
     * 3.0接口：：请求北京网关 3.20 取消套餐订单 专门用于For Shop
     * @param orderId
     * @param partnerCode
     * @param secret
     * @return
     */
    public CancelResponse cancel(String orderId, String partnerCode, String secret) {
        CancelRequest cancel = new CancelRequest();
        cancel.setVersion(JsonConstant.VERSION);
        cancel.setAccess_code(partnerCode);
        cancel.setTimestamp(TimeUtil.getTimeInMillis().toString());
        cancel.setOrder_id(orderId);
        try {
            cancel.setSign(HMACSHA1.jsonSign(cancel, secret));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }
        String requestCancel = JSON.toJSONString(cancel);
        log.info("请求【取消流量订单】：" + requestCancel);

        String url = jsonBaseURL3 + JsonConstant.JSON_SHOP_CANCEL_PACKAGE;
        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, requestCancel);
        log.info("url=" + url);

        log.info("响应【取消流量订单】：" + response);

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }

        return JSON.parseObject(response, CancelResponse.class);
    }

    /***
     * 3.0接口：：请求北京网关 3.22 提前结束套餐订单  专门用于For Shop
     *
     * @param orderId
     * @param partnerCode
     * @param secret
     * @param message
     * @return
     */
    public EndResponse end(String orderId, String partnerCode, String secret, String message) {
        EndRequest end = new EndRequest();
        end.setVersion(JsonConstant.VERSION);
        end.setAccess_code(partnerCode);
        end.setTimestamp(TimeUtil.getTimeInMillis().toString());
        end.setOrder_code(orderId);
        end.setAhead_finish_cause(message);
        try {
            end.setSign(HMACSHA1.jsonSign(end, secret));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }
        String requestEnd = JSON.toJSONString(end);
        log.info("请求【提前结束订单】：" + requestEnd);

        String url = jsonBaseURL3 + JsonConstant.JSON_SHOP_AHEAD_FINISH_PACKAGE;
        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, requestEnd);
        log.info("url=" + url);

        log.info("响应【提前结束订单】：" + response);

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }

        return JSON.parseObject(response, EndResponse.class);
    }

    /**
     * 3.0接口：：请求北京网关 3.10 订购套餐 专门用于For Shop
     * <p>
     * 老版：以payId作为唯一订单标志
     * <p>
     * 新版：以orderNo作为唯一订单标志
     * <p>
     * 最新版：加了payMode字段
     *
     * @param pur
     * @param isOpen
     * @return
     */
    public OrderResponse order(RequestPurchaseService pur, String isOpen) {
        OrderRequest order = new OrderRequest();
        order.setVersion(JsonConstant.VERSION);
        order.setAccess_code(pur.getPartnerCode());
        order.setTimestamp(TimeUtil.getTimeInMillis().toString());
        order.setDevice_id(pur.getDeviceCode());
        order.setGlobal_order("0");
        order.setPackage_name(pur.getPackageName());
        order.setPackage_code(pur.getPackageCode());
        order.setPackage_type(pur.getPackageType());
        order.setOrder_period(pur.getOrderPeriod());
        order.setPay_rst("2");
        order.setPay_type(pur.getPayType());
        order.setPay_amount(pur.getAmount());
        order.setIs_open(isOpen);
        order.setTest_flow_flag("0");
        order.setSerial_number(pur.getOrderNo());
        order.setPayMode(pur.getPayMode());
        try {
            order.setSign(HMACSHA1.jsonSign(order, pur.getSecret()));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }

        String requestOrder = JSON.toJSONString(order);
        log.info("请求【异步新订】：" + requestOrder);

        String url = jsonBaseURL3 + JsonConstant.JSON_ORDER_PACKAGE;
        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, requestOrder);
        log.info("url=" + url);

        log.info("响应【异步新订】：" + response);

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }

        return JSON.parseObject(response, OrderResponse.class);
    }

    /**
     * 3.0接口：：请求北京网关 3.12 续订流量套餐  专门用于For Shop
     * <p>
     * 老版：以payId作为唯一订单标志
     * <p>
     * 新版：以orderNo作为唯一订单标志
     * <p>
     * 最新版：加了payMode字段  方法的参数发生变化
     *
     * @param orderId
     * @param pur
     * @return
     */
    public RenewResponse reOrder(String orderId, RequestPurchaseService pur) {
        RenewRequest renew = new RenewRequest();
        renew.setVersion(JsonConstant.VERSION);
        renew.setAccess_code(pur.getPartnerCode());
        //返回响应时的UTC时间戳（秒），和互联网时间误差不能超过 5 分钟。
        renew.setTimestamp(TimeUtil.getTimeInMillis().toString());
        renew.setRenew_period(pur.getOrderPeriod());
        renew.setOrder_id(orderId);
        renew.setSerial_number(pur.getOrderNo());
        renew.setPayMode(pur.getPayMode());

        try {
            renew.setSign(HMACSHA1.jsonSign(renew, pur.getSecret()));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }
        String requestRenew = JSON.toJSONString(renew);
        log.info("请求【异步续订】：" + requestRenew);

        String url = jsonBaseURL3 + JsonConstant.JSON_RENEW_PACKAGE;
        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, requestRenew);
        log.info("url=" + url);

        log.info("响应【异步续订】：" + response);

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }

        return JSON.parseObject(response, RenewResponse.class);
    }

    /**
     * 3.0接口：：请求北京网关 ：3.1 同步资费套餐
     *
     * @param pageNo
     * @param pageSize
     * @param partnerCode
     * @param secret
     * @return
     */
    public ResponseSynchronizePackage synchronizePackage(Integer pageNo, Integer pageSize, String partnerCode, String secret) {
        RequestSynchronizePackage request = new RequestSynchronizePackage();
        request.setVersion(JsonConstant.VERSION);
        request.setAccess_code(partnerCode);
        request.setTimestamp(TimeUtil.getTimeInMillis().toString());
        if (pageNo != null) {
            request.setPage_no(pageNo);
        } else {
            request.setPage_no(1);
        }
        if (pageSize != null) {
            request.setPage_size(pageSize);
        } else {
            request.setPage_size(1000);
        }

        try {
            request.setSign(HMACSHA1.jsonSign(request, secret));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }

        String syncPackages = JSON.toJSONString(request);
        log.info("请求【同步资费套餐】：" + syncPackages);

        String url = jsonBaseURL3 + JsonConstant.JSON_SYNC_PACKAGES;
        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, syncPackages);
        log.info("url=" + url);
        //replaceAll("\\s*|\t|\r|\n", "")):java去除字符串中的空格、回车、换行符、制表符
        log.info("响应【同步资费套餐】：" + response.replaceAll("\\s*|\t|\r|\n", ""));

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }

        //这边只用验证返回的只要不是""证明请求响应是正常的
        return JSON.parseObject(response, ResponseSynchronizePackage.class);

    }

    /**
     * 3.0接口：：请求北京网关 3.24 批量同步设备可订购套餐
     *
     * @param pageNo
     * @param pageSize
     * @param partnerCode
     * @return
     */
    public ResponseSynchronizeDevice synchronizeDevice(Integer pageNo, Integer pageSize, String partnerCode, String secret) {
        RequestSynchronizeDevice request = new RequestSynchronizeDevice();
        request.setVersion(JsonConstant.VERSION);
        request.setAccess_code(partnerCode);
        request.setTimestamp(TimeUtil.getTimeInMillis().toString());
        request.setPartner_code(partnerCode);
        if (pageNo != null) {
            request.setPage_no(pageNo);
        } else {
            request.setPage_no(1);
        }
        if (pageSize != null) {
            request.setPage_size(pageSize);
        } else {
            request.setPage_size(1000);
        }

        try {
            request.setSign(HMACSHA1.jsonSign(request, secret));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }
        String syncDevice = JSON.toJSONString(request);
        log.info("请求【同步设备可购套餐】：" + syncDevice);

        String url = jsonBaseURL3 + JsonConstant.JSON_SYNC_DEVICE_PACKAGES;
        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, syncDevice);
        log.info("url=" + url);
        //replaceAll("\\s*|\t|\r|\n", "")):java去除字符串中的空格、回车、换行符、制表符
        log.info("响应【同步设备可购套餐】：" + response.replaceAll("\\s*|\t|\r|\n", ""));

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }

        //这边只用验证返回的只要不是""证明请求响应是正常的
        return JSON.parseObject(response, ResponseSynchronizeDevice.class);
    }

    /**
     * 3.0接口：：请求北京网关 ：3.17 同步企业信息
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public ResponseSynchronizePartner synchronizePartner(Integer pageNo, Integer pageSize) {
        RequestSynchronizePartner request = new RequestSynchronizePartner();
        request.setVersion(JsonConstant.VERSION);
        request.setAccess_code(JsonConstant.ACCESS_CODE);
        request.setTimestamp(TimeUtil.getTimeInMillis().toString());
        if (pageNo != null) {
            request.setPage_no(pageNo);
        } else {
            request.setPage_no(1);
        }
        if (pageSize != null) {
            request.setPage_size(pageSize);
        } else {
            request.setPage_size(1000);
        }

        try {
            request.setSign(HMACSHA1.jsonSign(request, JsonConstant.SECRET));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }

        String syncPartner = JSON.toJSONString(request);
        log.info("请求【同步企业】：" + syncPartner);
        String url = jsonBaseURL3 + JsonConstant.JSON_SYNC_PARTNER;

        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, syncPartner);
        log.info("url=" + url);
        //replaceAll("\\s*|\t|\r|\n", "")):java去除字符串中的空格、回车、换行符、制表符
        log.info("响应【同步企业】：" + response.replaceAll("\\s*|\t|\r|\n", ""));

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }
        //这边只用验证返回的只要不是""证明请求响应是正常的
        return JSON.parseObject(response, ResponseSynchronizePartner.class);
    }

    /**
     * 3.0接口：：请求北京网关3.3 查询单一设备实时信息
     *
     * @param deviceCode
     * @param partnerCode
     * @param secret
     * @return
     */
    public ResponseQueryDevice queryDevice(String deviceCode, String partnerCode, String secret) {
        RequestQueryDevice request = new RequestQueryDevice();
        request.setVersion(JsonConstant.VERSION);
        request.setAccess_code(partnerCode);
        request.setTimestamp(TimeUtil.getTimeInMillis().toString());
        request.setDevice_id(deviceCode);
        try {
            request.setSign(HMACSHA1.jsonSign(request, secret));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }

        String queryDevice = JSON.toJSONString(request);
        log.info("请求【查询设备】：" + queryDevice);

        String url = jsonBaseURL3 + JsonConstant.JSON_QUERY_DEVICE;
        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, queryDevice);
        log.info("url=" + url);
        String x = response.replaceAll("\\s*|\t|\r|\n", "");
        //replaceAll("\\s*|\t|\r|\n", "")):java去除字符串中的空格、回车、换行符、制表符
        log.info("响应【查询设备】：" + response.replaceAll("\\s*|\t|\r|\n", ""));

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }

        //这边只用验证返回的只要不是""证明请求响应是正常的
        return JSON.parseObject(response, ResponseQueryDevice.class);
    }

    /**
     * 3.0接口：：请求北京网关 3.16 查询套餐订单信息
     *
     * @param deviceCode
     * @param orderId
     * @param orderStatus
     * @param orderType
     * @param partnerCode
     * @param secret
     * @return
     */
    public ResponseQueryDeviceOrder queryDeviceOrder(String deviceCode, String orderId, String orderStatus, String orderType, String partnerCode, String secret) {
        RequestQueryDeviceOrder request = new RequestQueryDeviceOrder();
        request.setVersion(JsonConstant.VERSION);
        request.setTimestamp(TimeUtil.getTimeInMillis().toString());
        request.setAccess_code(partnerCode);
        request.setDevice_id(deviceCode);

        if (StringUtils.isNotBlank(orderId)) {
            request.setOrder_id(orderId);
        }
        if (StringUtils.isNotBlank(orderType)) {
            request.setOrder_type(orderType);
        }
        if (StringUtils.isNotBlank(orderStatus)) {
            request.setOrder_status(orderStatus);
        }
        request.setPage_no(1);
        request.setPage_size(500);
        try {
            request.setSign(HMACSHA1.jsonSign(request, secret));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }
        String queryOrder = JSON.toJSONString(request);
        String response;

        if (StringOutputConstant.CODE_TWO.equals(orderStatus)) {
            log.info("请求查询【正在使用】订单:" + queryOrder);
            //3.0重构的接口
            String url = jsonBaseURL3 + JsonConstant.JSON_QUERY_H5_ORDER;

            // 发送post请求，并接收返回结果
            response = HttpUtil.httpPost(url, queryOrder);
            log.info("url=" + url);
            //replaceAll("\\s*|\t|\r|\n", "")):java去除字符串中的空格、回车、换行符、制表符
            log.info("响应查询【正在使用】订单：" + response.replaceAll("\\s*|\t|\r|\n", ""));
        } else if (StringOutputConstant.CODE_ONE.equals(orderStatus)) {
            log.info("请求查询【未启用】订单:" + queryOrder);
            //3.0重构的接口
            String url = jsonBaseURL3 + JsonConstant.JSON_QUERY_H5_ORDER;

            // 发送post请求，并接收返回结果
            response = HttpUtil.httpPost(url, queryOrder);
            log.info("url=" + url);
            //replaceAll("\\s*|\t|\r|\n", "")):java去除字符串中的空格、回车、换行符、制表符
            log.info("响应查询【未启用】订单：" + response.replaceAll("\\s*|\t|\r|\n", ""));
        } else if (StringOutputConstant.CODE_THREE.equals(orderStatus)) {
            log.info("请求查询【已结束】订单:" + queryOrder);
            //3.0重构的接口
            String url = jsonBaseURL3 + JsonConstant.JSON_QUERY_H5_ORDER;

            // 发送post请求，并接收返回结果
            response = HttpUtil.httpPost(url, queryOrder);
            log.info("url=" + url);
            //replaceAll("\\s*|\t|\r|\n", "")):java去除字符串中的空格、回车、换行符、制表符
            log.info("响应查询【已结束】订单：" + response.replaceAll("\\s*|\t|\r|\n", ""));
        } else {
            log.info("请求查询【所有】订单:" + queryOrder);
            //3.0重构的接口
            String url = jsonBaseURL3 + JsonConstant.JSON_QUERY_H5_ORDER;

            // 发送post请求，并接收返回结果
            response = HttpUtil.httpPost(url, queryOrder);
            log.info("url=" + url);
            //replaceAll("\\s*|\t|\r|\n", "")):java去除字符串中的空格、回车、换行符、制表符
            log.info("响应查询【所有】订单：" + response.replaceAll("\\s*|\t|\r|\n", ""));
        }
        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }

        //这边只用验证返回的只要不是""证明请求响应是正常的
        return JSON.parseObject(response, ResponseQueryDeviceOrder.class);
    }

    /**
     * 3.0接口：：请求北京网关 3.25 根据设备标识查询所属企业 For Shop
     *
     * @param deviceCode
     * @return
     */
    public ResponseQueryPartnerByDeviceId queryPartnerCode(String deviceCode) {
        RequestQueryPartnerByDeviceId request = new RequestQueryPartnerByDeviceId();
        request.setVersion(JsonConstant.VERSION);
        request.setAccess_code(JsonConstant.ACCESS_CODE);
        request.setTimestamp(TimeUtil.getTimeInMillis().toString());
        request.setDevice_id(deviceCode);
        try {
            request.setSign(HMACSHA1.jsonSign(request, JsonConstant.SECRET));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }

        String queryPartner = JSON.toJSONString(request);
        log.info("请求【查询企业编码】：" + queryPartner);
        String url = jsonBaseURL3 + JsonConstant.JSON_QUERY_PARTNER_CODE;

        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, queryPartner);
        log.info("url=" + url);

        //replaceAll("\\s*|\t|\r|\n", "")):java去除字符串中的空格、回车、换行符、制表符
        log.info("响应【查询企业编码】：" + response.replaceAll("\\s*|\t|\r|\n", ""));

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }
        //这边只用验证返回的只要不是""证明请求响应是正常的
        return JSON.parseObject(response, ResponseQueryPartnerByDeviceId.class);
    }

    /**
     * 3.0接口：：请求北京网关 3.11 开通流量套餐 专门用于For Shop
     *
     * @param orderId
     * @param secret
     * @param record
     * @return
     */
    public ResponseOpenPackage openPackageForShop(String orderId, String secret, ServicePlanOrderRecord record) {
        /*3.0的openPackageForShop接口不用传payMode字段*/
        RequestOpenPackage request = new RequestOpenPackage();
        request.setVersion(JsonConstant.VERSION);
        request.setAccess_code(record.getPartnerCode());
        request.setTimestamp(TimeUtil.getTimeInMillis().toString());
        request.setOrder_id(orderId);
        try {
            request.setSign(HMACSHA1.jsonSign(request, secret));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }

        String openPackage = JSON.toJSONString(request);
        log.info("请求【开通套餐】：" + openPackage);
        //说明接口从2.0更新到3.0了
        String url = jsonBaseURL3 + JsonConstant.JSON_OPEN_PACKAGES_FOR_SHOP;

        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, openPackage);
        log.info("url=" + url);
        //replaceAll("\\s*|\t|\r|\n", "")):java去除字符串中的空格、回车、换行符、制表符
        log.info("响应【开通套餐】：" + response.replaceAll("\\s*|\t|\r|\n", ""));

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }

        //这边只用验证返回的只要不是""证明请求响应是正常的
        return JSON.parseObject(response, ResponseOpenPackage.class);
    }


    /**
     * 2.0接口：：请求北京网关3.27 H5 查询套餐
     *
     * @param deviceCode
     * @param orderId
     * @return
     */
    public ResponseQueryH5Order queryOrder(String deviceCode, String orderId, String orderStatus, String orderType, String partnerCode, String secret) {
        RequestQueryH5Order request = new RequestQueryH5Order();
        request.setVersion(JsonConstant.VERSION);
        request.setAccess_code(partnerCode);
        request.setTimestamp(TimeUtil.getTimeInMillis().toString());
        request.setDevice_id(deviceCode);
        if (StringUtils.isNotBlank(orderId)) {
            request.setOrder_id(orderId);
        }
        if (StringUtils.isNotBlank(orderType)) {
            request.setOrder_type(orderType);
        }
        request.setPage_no(1);
        request.setPage_size(1000);

        if (StringUtils.isNotBlank(orderStatus)) {
            if (StringOutputConstant.CODE_TWO.equals(orderStatus)) {
                request.setOrder_status(orderStatus);
                try {
                    request.setSign(HMACSHA1.jsonSign(request, secret));
                } catch (Exception e) {
                    log.info("计算签名出现异常：" + e);
                }
                /*查询正在使用的订单*/
                String queryOrder = JSON.toJSONString(request);
                log.info("请求查询【正在使用】订单：" + queryOrder);

                String url = jsonBaseURL2 + JsonConstant.JSON_QUERY_H5_ORDER;
                // 发送post请求，并接收返回结果
                String response = HttpUtil.httpPost(url, queryOrder);
                log.info("url=" + url);

                log.info("响应查询【正在使用】订单：" + response.replaceAll("\\s*|\t|\r|\n", ""));
                if (StringUtils.isBlank(response)) {
                    return null;
                }
                //这边只用验证返回的只要不是""证明请求响应是正常的
                return JSON.parseObject(response, ResponseQueryH5Order.class);

            } else if (StringOutputConstant.CODE_ONE.equals(orderStatus)) {
                request.setOrder_status(orderStatus);
                try {
                    request.setSign(HMACSHA1.jsonSign(request, secret));
                } catch (Exception e) {
                    log.info("计算签名出现异常：" + e);
                }
                /*查询未启用的订单*/
                String queryOrder = JSON.toJSONString(request);
                log.info("请求查询【未启用】订单：" + request);

                String url = jsonBaseURL2 + JsonConstant.JSON_QUERY_H5_ORDER;
                // 发送post请求，并接收返回结果
                String response = HttpUtil.httpPost(url, queryOrder);
                log.info("url=" + url);

                log.info("响应查询【未启用】订单：" + response.replaceAll("\\s*|\t|\r|\n", ""));
                if (StringUtils.isBlank(response)) {
                    return null;
                }
                //这边只用验证返回的只要不是""证明请求响应是正常的
                return JSON.parseObject(response, ResponseQueryH5Order.class);
            } else if (StringOutputConstant.CODE_THREE.equals(orderStatus)) {
                request.setOrder_status(orderStatus);
                try {
                    request.setSign(HMACSHA1.jsonSign(request, secret));
                } catch (Exception e) {
                    log.info("计算签名出现异常：" + e);
                }
                /*查询未启用的订单*/
                String queryOrder = JSON.toJSONString(request);
                log.info("请求查询【已结束】订单：" + request);

                String url = jsonBaseURL2 + JsonConstant.JSON_QUERY_H5_ORDER;
                // 发送post请求，并接收返回结果
                String response = HttpUtil.httpPost(url, queryOrder);
                log.info("url=" + url);

                log.info("响应查询【已结束】订单：" + response.replaceAll("\\s*|\t|\r|\n", ""));
                if (StringUtils.isBlank(response)) {
                    return null;
                }
                //这边只用验证返回的只要不是""证明请求响应是正常的
                return JSON.parseObject(response, ResponseQueryH5Order.class);
            }

        }
        /*查询所有状态的订单*/
        try {
            request.setSign(HMACSHA1.jsonSign(request, secret));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }
        //查询有没有正在使用的订单
        String queryOrder = JSON.toJSONString(request);
        log.info("请求查询【所有状态】订单：" + queryOrder);

        String url = jsonBaseURL2 + JsonConstant.JSON_QUERY_H5_ORDER;
        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, queryOrder);
        log.info("url=" + url);

        log.info("响应查询【所有状态】订单：" + response.replaceAll("\\s*|\t|\r|\n", ""));
        if (StringUtils.isBlank(response)) {
            return null;
        }

        //这边只用验证返回的只要不是""证明请求响应是正常的
        return JSON.parseObject(response, ResponseQueryH5Order.class);
    }


    /**
     * 2.0接口：：请求北京网关 3.12 续订流量套餐  专门用于For Shop
     * <p>
     * 老版：以payId作为唯一订单标志
     * <p>
     * 新版：以orderNo作为唯一订单标志
     *
     * @param orderPeriod
     * @param orderId
     * @param partnerCode
     * @param serialNumber
     * @param secret
     * @return
     */
    public RenewResponse reOrder(String orderPeriod, String orderId, String partnerCode, String serialNumber, String secret) {
        RenewRequest renew = new RenewRequest();
        renew.setVersion(JsonConstant.VERSION);
        renew.setAccess_code(partnerCode);
        //返回响应时的UTC时间戳（秒），和互联网时间误差不能超过 5 分钟。
        renew.setTimestamp(TimeUtil.getTimeInMillis().toString());
        renew.setRenew_period(orderPeriod);
        renew.setOrder_id(orderId);
        renew.setSerial_number(serialNumber);
        try {
            renew.setSign(HMACSHA1.jsonSign(renew, secret));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }
        String requestRenew = JSON.toJSONString(renew);
        log.info("请求【异步续订】：" + requestRenew);
        Integer c = logicAssistService.queryUpgradedOrNot(partnerCode);
        String url;
        if (c > 0) {
            //说明接口从2.0更新到3.0了
            url = jsonBaseURL3 + JsonConstant.JSON_RENEW_PACKAGE;
        } else {
            url = jsonBaseURL2 + JsonConstant.JSON_RENEW_PACKAGE;
        }
        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, requestRenew);
        log.info("url=" + url);

        log.info("响应【异步续订】：" + response);

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }

        return JSON.parseObject(response, RenewResponse.class);
    }

    /**
     * 2.0接口：：请求北京网关 3.11 开通流量套餐  老版本！！！
     *
     * @param partnerCode
     * @param orderId
     * @param secret
     * @return
     */
    public ResponseOpenPackage openPackage(String partnerCode, String orderId, String secret) {
        RequestOpenPackage request = new RequestOpenPackage();
        request.setVersion(JsonConstant.VERSION);
        request.setAccess_code(partnerCode);
        request.setTimestamp(TimeUtil.getTimeInMillis().toString());
        request.setOrder_id(orderId);

        try {
            request.setSign(HMACSHA1.jsonSign(request, secret));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }

        String openPackage = JSON.toJSONString(request);
        log.info("请求【开通套餐】：" + openPackage);
        String url = jsonBaseURL2 + JsonConstant.JSON_OPEN_PACKAGES;

        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, openPackage);
        log.info("url=" + url);
        //replaceAll("\\s*|\t|\r|\n", "")):java去除字符串中的空格、回车、换行符、制表符
        log.info("响应【开通套餐】：" + response.replaceAll("\\s*|\t|\r|\n", ""));

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }

        //这边只用验证返回的只要不是""证明请求响应是正常的
        return JSON.parseObject(response, ResponseOpenPackage.class);
    }

    /**
     * 2.0接口：：请求北京网关 3.11 开通流量套餐 专门用于For Shop 新版本！！！
     *
     * @param orderId
     * @param secret
     * @param record
     * @return
     */
    public ResponseOpenPackage openPackage(String orderId, String secret, ServicePlanOrderRecord record) {
        /*2.0的openPackageForShop接口需要用传payMode字段*/
        RequestOpenPackage request = new RequestOpenPackage();
        request.setVersion(JsonConstant.VERSION);
        request.setAccess_code(record.getPartnerCode());
        request.setTimestamp(TimeUtil.getTimeInMillis().toString());
        request.setOrder_id(orderId);
        request.setPayMode(record.getPayMode());

        try {
            request.setSign(HMACSHA1.jsonSign(request, secret));
        } catch (Exception e) {
            log.info("计算签名出现异常：" + e);
        }

        String openPackage = JSON.toJSONString(request);
        log.info("请求【开通套餐】：" + openPackage);
        String url = jsonBaseURL2 + JsonConstant.JSON_OPEN_PACKAGES_FOR_SHOP;

        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, openPackage);
        log.info("url=" + url);
        //replaceAll("\\s*|\t|\r|\n", "")):java去除字符串中的空格、回车、换行符、制表符
        log.info("响应【开通套餐】：" + response.replaceAll("\\s*|\t|\r|\n", ""));

        //这边验证返回的是"",那么为了程序不报错给返回一个null
        if (StringUtils.isBlank(response)) {
            return null;
        }

        //这边只用验证返回的只要不是""证明请求响应是正常的
        return JSON.parseObject(response, ResponseOpenPackage.class);
    }
}
